package com.yuan.zs.ui.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.bean.TipResult;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.utils.DialogUtil;
import com.yuan.base.utils.PreferencesUtils;
import com.yuan.zs.Constant;
import com.yuan.zs.R;
import com.yuan.zs.Urls;
import com.yuan.zs.ui.LoginActivity;

import java.util.HashMap;

public class ApplyParentBindingActivity extends BaseFrameworkActivity {

	private EditText et_parent_name;
	private EditText et_parent_id_card;
	private TextView tv_parent_sex;
	private TextView tv_is_main_account;
	private EditText et_child_name;
	private EditText et_child_id_card;
	private EditText et_child_relationship;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_apply_parent;
	}

	@Override
	protected void bindViews() {
		et_parent_name = findViewById(R.id.et_parent_name);
		et_parent_id_card = findViewById(R.id.et_parent_id_card);
		tv_parent_sex = findViewById(R.id.tv_parent_sex);
		tv_is_main_account = findViewById(R.id.tv_is_main_account);
		et_child_name = findViewById(R.id.et_child_name);
		et_child_id_card = findViewById(R.id.et_child_id_card);
		et_child_relationship = findViewById(R.id.et_child_relationship);

		tv_parent_sex.setOnClickListener(v -> selectSex());
		tv_is_main_account.setOnClickListener(v -> selectIsMain());
		setOnClickListener(R.id.btn_submit, v -> doSubmit());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public void closeSelf() {
		Intent intent = new Intent(this, LoginActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		super.closeSelf();
	}

	private void selectSex() {
		String[] sex = new String[]{"男", "女"};
		DialogUtil.select(this, "选择性别", sex, (dialog, which) -> tv_parent_sex.setText(sex[which]));
	}

	private void selectIsMain() {
		String[] sex = new String[]{"是", "否"};
		DialogUtil.select(this, "选择是否主账号", sex, (dialog, which) -> tv_is_main_account.setText(sex[which]));
	}

	private void doSubmit() {
		if (TextUtils.isEmpty(getStringTrimByUI(et_parent_name))) {
			showToast("请填写家长姓名");
			return;
		}
		if (TextUtils.isEmpty(getStringTrimByUI(et_parent_id_card))) {
			showToast("请填写家长身份证");
			return;
		}
		if (TextUtils.isEmpty(getStringTrimByUI(et_child_name))) {
			showToast("请填写幼儿姓名");
			return;
		}
		if (TextUtils.isEmpty(getStringTrimByUI(et_child_id_card))) {
			showToast("请填写幼儿身份证");
			return;
		}
		if (TextUtils.isEmpty(getStringTrimByUI(et_child_relationship))) {
			showToast("请填写与幼儿关系");
			return;
		}
		HashMap<String, Object> params = new HashMap<>();
		params.put("name", getStringTrimByUI(et_parent_name));
		params.put("idCardNo", getStringTrimByUI(et_parent_id_card));
		params.put("gender", getStringTrimByUI(tv_parent_sex));
		params.put("phone1", PreferencesUtils.getString(this, Constant.Preferences.USER_CODE, ""));
		params.put("childName", getStringTrimByUI(et_child_name));
		params.put("childIdCardNo", getStringTrimByUI(et_child_id_card));
		params.put("relationship", getStringTrimByUI(et_child_relationship));
		params.put("principal", getStringTrimByUI(tv_is_main_account).equals("是"));

		BaseHttpRequest.post(Urls.getUrl(Urls.applyParentBinding), params, new BaseHttpCallBack<TipResult>() {
			@Override
			public void onPost() {
				hideProgressBarAtOnce();
			}

			@Override
			public void onPreStart() {
				showProgressBar("正在提交...");
			}

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("申请成功");
					closeSelf();
				} else {
					showToast(result.getMessage());
				}
			}
		});
	}


}
